package com.scx.run;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scx.bean.db.Columns;
import com.scx.bean.db.DataBase;
import com.scx.bean.db.DataSourceConfig;
import com.scx.bean.db.Tables;
import com.scx.bean.out.Table;
import com.scx.bean.out.TableCarray;
import com.scx.oper.DBFactoryTable;
import com.scx.oper.FreemarkerFileWriterFactory;
import com.scx.util.Iconst;
import com.scx.util.StringUtil;


public class MybatisCreate {
	
	// dto 包名
	private static final String dtoPACKAGENAME = "mapper";
	// dao 包名
	private static final String daoPACKAGENAME = "tabledao";
	// dto输出路径
	private static final String DTOOUTPUTPATH = "C:\\Users\\scx\\Desktop\\SSM\\src\\main\\java\\mapper\\";
	// dao输入路径
	private static final String DAOOUTPUTPATH = "C:\\Users\\scx\\Desktop\\SSM\\src\\main\\java\\tabledao\\";
	// mybatis配置文件
	private static final String IBATIS_CONFIG_FILE = "C:\\Users\\scx\\Desktop\\SSM\\src\\main\\resources\\conf\\mybatis-config.xml";
	// 需要生成出来的表名
	private static String[] outTableNames = {};
	
	
	public static void createFiles() {
		// 数据库连接
		DataSourceConfig dbInfo = new DataSourceConfig();
		dbInfo.setIp("127.0.0.1");
		dbInfo.setPort("3306");
		dbInfo.setDbName("test");
		dbInfo.setUser("root");
		dbInfo.setPass("root");
		DBFactoryTable dbFactory = new DBFactoryTable(Iconst.DB_MYSQL, dbInfo);

		// mybatis配置信息
		List<Map<String, String>> listIbatisConfigStr = new ArrayList<Map<String, String>>();

		// 获取数据库信息
		DataBase databaseInfo = dbFactory.chooseDatabase();
		List<Tables> listTables = databaseInfo.getListTables();
		for (int i = 0; i < listTables.size(); i++) {
			// 表信息
			Tables table_d = listTables.get(i);
			// 判断是否需要生成的表
			if (outTableNames.length != 0) {
				if (!Arrays.asList(outTableNames).contains(table_d.getTableName())) {
					continue;
				}
			}

			// 数据转换生成模版可识别的数据结构
			Table table = new Table();
			List<TableCarray> listTableCarrays = new ArrayList<TableCarray>();
			table.setPojoPackage(dtoPACKAGENAME); // dto包名字
			table.setDaoPackage(daoPACKAGENAME); // dao包名字
			table.setClassName(table_d.getTableName()); // 原始类名
			// 大写类名
			table.setClassName_d(StringUtil.removalUnderlinedUpperFirst(table_d.getTableName()));
			// 首字母小写类名
			table.setClassName_x(StringUtil.lowerFirst(StringUtil.removalUnderlinedUpperFirst(table_d.getTableName())));
			// 处理数据结构
			List<Columns> listCoun = table_d.getListColumns();
			for (int j = 0; j < listCoun.size(); j++) {
				Columns pdmColumn = listCoun.get(j);
				TableCarray tableCarray = new TableCarray();
				if (j == 0) {
					// 设置当前主键字段 -默认第一个字段
					table.setPrimaryKeyCode(pdmColumn.getColumnName());
				}
				// 字段描述
				tableCarray.setCarrayName(pdmColumn.getRemarks());
				// 字段名，大写
				tableCarray.setCarrayName_d(StringUtil.upperFirst(pdmColumn.getColumnName()));
				// 字段名，小写
				tableCarray.setCarrayName_x(pdmColumn.getColumnName());
				// 字段类型 --全部定义为String
				tableCarray.setCarrayType("String");
				// 添加到list集合中
				listTableCarrays.add(tableCarray);
			}
			// 设置到table中
			table.setTableCarrays(listTableCarrays);

			// 生成DTO类
			FreemarkerFileWriterFactory.dataSourceOut(table, "mybatis/dto.ftl", DTOOUTPUTPATH + table.getClassName_d() + "Dto.java");
			// 生成mybatis的XML
			FreemarkerFileWriterFactory.dataSourceOut(table, "mybatis/mybatis_xml.ftl", DTOOUTPUTPATH + table.getClassName_d() + "Dto.xml");
			// 生成dao
			FreemarkerFileWriterFactory.dataSourceOut(table, "mybatis/dao.ftl", DAOOUTPUTPATH + table.getClassName_d() + "IDao.java");
			// 打印mybatis路径
			Map<String, String> map = new HashMap<String, String>(); 
			map.put("name", table.getClassName_d());
			map.put("path", dtoPACKAGENAME);
			listIbatisConfigStr.add(map);
		}
		// 重置ibatisConfig
		Table tableIbatisConfig = new Table();
		tableIbatisConfig.setIbatisConfigStr(listIbatisConfigStr);
		FreemarkerFileWriterFactory.dataSourceOut(tableIbatisConfig, "mybatis/mybatis_config.ftl", IBATIS_CONFIG_FILE);
		/* ----------- */
		System.out.println("生成结束");
	}
	
	public static void main(String[] args) {
		createFiles();
	}
}
